<?php
$menuEditPath = ADMIN_DIR.'/topMenu.php';
$menuInclPath = _DOCUMENT_ROOT.$menuEditPath;
include $menuInclPath;

function menuFromAr( $menuAr, $subPath='' ){
	global $onClickAttr;
	$res = '';
	foreach( $menuAr as $title=>$value){
		$uri = is_array($value) ? $value[0] : $value;
		$uri = $subPath.$uri;
		
		$active = $uri=='/' ? ( $_SERVER['REQUEST_URI']=='/' ? 'class="active"' : '' ) : (strpos( $_SERVER['REQUEST_URI'], $uri )===0 ? 'class="active"' : '');
		if( $active!='' ){/* bread crumbs */
			global $topMenuBreadCrumbs, $sectionArray; 
			$topMenuBreadCrumbs[ $uri ] = $title;
			if(is_array($value)){
				$sectionArray = array();
				foreach( $value[1] as $stitle=>$link ) $sectionArray[ $stitle ] = $uri.$link;
			} 
		}
		$menuLink = $_SERVER['REQUEST_URI']!=$uri ? $uri : '#" title="Вы уже здесь."' ;
		if(is_array($value)){
			$res.='<li '.$active.'><a '.$onClickAttr.' href="'.$menuLink.'">'.$title.'</a>'."\n".
				'<div><ul class="top_submenu _right shadow">'.menuFromAr( $value[1], $uri ).'</ul></div>'."\n".
				'</li>'."\n";	
		}else{
			$res.='<li '.$active.'><a '.$onClickAttr.' href="'.$menuLink.'">'.$title.'</a></li>'."\n";
		}
	}
	return $res;
}

function createSectionMenu( $sectionArray ){ //myPrint($sectionArray, '$sectionArray');
	foreach( $sectionArray as $title=>$link){
		$pi = pathinfo($link);
		$result.='<div class="panel section_but">'.
		'<a '.$onClickAttr.' class="'.strtolower($pi['filename']).'" href="'.$link.'">'.$title.'</a></div>';
	}
		
	$result.='<div style="clear:both;"></div>';
	return $result;
}

global $topMenuBreadCrumbs, $sectionArray, $onClickAttr; 
$onClickAttr = 'onclick="if(typeof checkNoSavedAnchor==\'function\') return checkNoSavedAnchor(this);"';	
$menuFromAr = menuFromAr( $topMenuAr );
?>
<form action="/" method="post" name="logout"><input type="hidden" name="logout" value="1"></form>
<div class="top_menu shadow">
	<?php if( checkAccess( array('1') ) ) {?>
		<a onclick="window.open( this.href); return false;" title="Редактировать..." class="edit_but" style="float:right;" href="/admin/modules/content/TextFileEdit.php?file_path=<?php echo $menuEditPath?>">&nbsp;</a>
	<?php }?>
	<table style="margin_: 0 auto;"><tr><td>
		<div>
		<ul>
			<?php
				echo $menuFromAr;
			?>	
			<li><a href="#" onclick="if(confirm('Точно выйти?..')) document.forms.logout.submit();">Выйти...</a></li>	
		</ul>
		</div>
	</td></tr></table>
	<div class="clear_both"></div>
</div>

<?php
	/* навигац. цепочка */
	$rootPageTitle = 'Управление сайтом'; 
	$breadCrumbs = array('<a '.$onClickAttr.' href="/admin">'.$rootPageTitle.'</a>');
	foreach ($topMenuBreadCrumbs as $link=>$title) $breadCrumbs[] ='<a '.$onClickAttr.' href="'.$link.'">'.$title.'</a>';
	if(count( $breadCrumbs )>1){ //$title = $rootPageTitle;
		$last = array_pop( $breadCrumbs );
		$breadCrumbs[] = '<b style="color:navy" title="Вы здесь">'.$title.'</b>';		
		echo '<div class="panel inline-block">'.implode( $breadCrumbs, ' &rArr; ').'</div><br>';
	}
	
	/* заголовок */
	if(count($topMenuBreadCrumbs)>0) $pageTitle = array_pop( $topMenuBreadCrumbs );
	else $pageTitle = $rootPageTitle;
	
	/* меню раздела */	
	if(count($breadCrumbs)<=2) {
		echo '<h2 class="">'.$pageTitle.'</h2>';//myPrint( $breadCrumbs, '$breadCrumbs' );
		echo createSectionMenu( $sectionArray );
	}
?>